﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Repositories;
using DomenModel;
using System.Data.Entity;
using System.Linq.Expressions;
using Model.Repositories.Interfaces;




namespace Infrastructure.Repositories
{
    public class CreditCardRepo: ICrud<CreditCard>
    {
        /// <summary>
        /// Gets the CreditCard by id.
        /// </summary>
        /// <param name="Id">The id.</param>
        /// <returns></returns>
        public CreditCard GetById(long Id)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                var creditCardById = _db.CreditCards.FirstOrDefault(p => p.CreditCardId == Id);
                return creditCardById;
            }
        }

        /// <summary>
        /// Saves the CreditCard to Data Base.
        /// </summary>
        /// <param name="t">The t.</param>
        public void Save(CreditCard t)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                _db.CreditCards.Add(t);
                _db.SaveChanges();
            }
        }

        /// <summary>
        /// Updates the CreditCard.
        /// </summary>
        /// <param name="t">The t.</param>
        public void Update(CreditCard t)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                var creditCardById = _db.CreditCards.FirstOrDefault(p => p.CreditCardId == t.CreditCardId);
                creditCardById.CardNumber = t.CardNumber;
                creditCardById.CardType = t.CardType;
                creditCardById.CVV2Code = t.CVV2Code;
                creditCardById.ExpiryDate = t.ExpiryDate;
                creditCardById.StartDate = t.StartDate;
                creditCardById.IssueNumber = t.IssueNumber;
                creditCardById.Orders = t.Orders;
                _db.SaveChanges();
            }
        }

        /// <summary>
        /// Deletes the CreditCard from Data Base.
        /// </summary>
        /// <param name="Id">The id.</param>
        public void Delete(long Id)
        {
            using (GeneralContext _db = new GeneralContext())
            {
                var creditCardById = _db.CreditCards.FirstOrDefault(p => p.CreditCardId == Id);
                _db.CreditCards.Remove(creditCardById);
                _db.SaveChanges();
            }
        }

        /// <summary>
        /// Gets the collection of all CreditCard.
        /// </summary>
        /// <returns></returns>
        public List<CreditCard> GetCollection()
        {
            using (GeneralContext _db = new GeneralContext())
            {
                var listOfCreditCard = _db.CreditCards.ToList();
                return listOfCreditCard;
            };
        }

     }
}
